package com
{
	import com.assist.Helper;
	import com.assist.data.IData;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.Socket;

	public class DataBase extends SuperBase implements IData
	{
		protected var _ctrl :Controller;
		protected var _view :View;
		
		protected var _sock :Socket;
		protected var _onConnect:Function;
		protected var _onClose:Function;
		
		private var _host:String;
		private var _port:int;
		private var connectSucceed:Boolean = false;
		
		public function DataBase()
		{
			super();
			
			_sock = new Socket();
		}
		
		public function init(view:View, controller:Controller) : void
		{
			_view = view;
			_ctrl = controller;
		}
		
		public function set onConnect(connectFunc:Function) : void
		{
			_onConnect = connectFunc;
		}
		
		public function connect(host:String, port:int) : void
		{
			_host = host;
			_port = port;
			addListener();
			if (_sock.connected)
			{
				_sock.close();
			}
			_sock.connect(_host, _port);
		}
		
		private function addListener() : void
		{
			_sock.addEventListener(Event.CONNECT, onConnectHandler);
			_sock.addEventListener(IOErrorEvent.IO_ERROR, onIoErrorHandler);
//			_sock.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityErrorHandler);
//			_sock.addEventListener(ProgressEvent.SOCKET_DATA, onSocketDataHandler);
			_sock.addEventListener(Event.CLOSE, onCloseHandler);
		}
		
		private function removeListener() : void
		{
			_sock.removeEventListener(Event.CONNECT, onConnectHandler);
			_sock.removeEventListener(IOErrorEvent.IO_ERROR, onIoErrorHandler);
//			_sock.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityErrorHandler);
//			_sock.removeEventListener(ProgressEvent.SOCKET_DATA, onSocketDataHandler);
			_sock.removeEventListener(Event.CLOSE, onCloseHandler);
		}
		
		private function onConnectHandler(event:Event) : void {
			onConnectHandlerReal();
		}
		
		private function onIoErrorHandler(event:IOErrorEvent) : void
		{
			Helper.backtrace("onIoErrorHandler");
			//this._view.alert.socketClosed("连接不到服务器！", "Socket 错误。 URL: " + this._host + ":" + this._port + "，该地址没有提供Socket服务。");
			//this._view.resetHost();
		}
		
		private function onCloseHandler(event:Event) : void
		{
			Helper.backtrace("onCloseHandler");
			//this._view.tip2.showAlert(DataBaseLang.connected);
			//this._view.resetHost();
			if (_onClose is Function)
			{
				_onClose();
			}
		}
		
		private function onConnectHandlerReal() : void
		{
			Helper.backtrace("onConnectHandler");
			connectSucceed = true;
			if (_onConnect is Function)
			{
				_onConnect();
				_onConnect = null;
			}
		}
		
		public function call(param1:Object, param2:Function, param3:Array, param4:Boolean=true):void
		{
		}
		
		public function patch(param1:Object, param2:Function):void
		{
		}
		
		public function cancelPatch(param1:Object):void
		{
		}
		
		public function set onClose(param1:Function):void
		{
		}
		
		public function close():void
		{
		}
	}
}